package com.renhui.android.handwriting.hermes;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Binder;
import android.os.IBinder;
import android.os.Messenger;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import com.renhui.android.handwriting.R;
import com.renhui.android.handwriting.common.PReflectionUtils;
import com.renhui.android.handwriting.service.TestService;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import xiaofei.library.hermeseventbus.HermesEventBus;

public class EvenMulitEventBusActivity extends AppCompatActivity {

    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 测试 Android P 的反射
        PReflectionUtils.getWifiReflection(this);

        HermesEventBus.getDefault().register(this);
        setContentView(R.layout.activity_even_mulit_event_bus);
        textView = findViewById(R.id.text);
        findViewById(R.id.send_message).setOnClickListener(v -> {
            startActivity(new Intent(EvenMulitEventBusActivity.this, EvenMulitProcessActivity.class));
        });

        ServiceConnection connection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                //Binder binder = (Binder) service;
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {

            }
        };
        startService(new Intent(this, TestService.class));
        bindService(new Intent(this, TestService.class), connection, Service.BIND_AUTO_CREATE);
        unbindService(connection);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventBusCome(String message) {
        Log.e("EventBus",  Thread.currentThread().getName());
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
        textView.setText(message);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        HermesEventBus.getDefault().destroy();
    }
}